# 对应CSDN python系列专栏博客
# 对应2024/04/06 的python博客
# 主题：字符串处理技巧大揭秘：从基础到高级

# 二、创建字符串
# 2.1.1 使用赋值运算符创建
str1 = 'hello str1'
str2 = "hello world str2"
str3 = """
# hello
# world
# str3
"""
str4 = '''
hello 
world
str4
'''
print(str1, end='-')
print(str2, end='-')
print(str3, end='-')
print(str4, end='-')

# 2.1.2 使用str() 或 repr()
print(str(8.42))
print(str(True))
print(str([4, 2, 9, 8]))

num = 42
repr_num = repr(num)
print(repr_num)

# 2.2 字符串的访问
# 2.2.1 索引
my_str = "CILMY23"
print(my_str[0])
print(my_str[-1])

# 2.2.2 切片
my_str = "CILMY23"
print(my_str[2:5])
print(my_str[:])
print(my_str[2:5:2])
print(my_str[-1:-3:-1])
print(my_str[::-1]) # 旋转输出字符串
print(my_str[-1:-3:-2])
print(my_str[-1:-3:1]) # 无法打印

my_str = "CILMY23"
for char in my_str:
    print(char)

# 2.3 字符串运算
# 2.3.1 拼接字符串
str1 = "CI"
str2 = "LMY"
str3 = "23"
connect_str = str1 + str2 + str3
print(connect_str)
# 2.3.2 重复打印字符串
print(connect_str * 3)

my_str = "CILMY23"
str1 = "CI"
str2 = "LMY"
str3 = "23"
str4 = "12"
# 使用 in 运算符检查子串是否存在
print(str1 in my_str)
print(str2 in my_str)

# 使用 not in 运算符检查子串是否不存在
print(str1 not in my_str)
print(str4 not in my_str)

# 三、字符串函数
# 3.1 字符串查找
# 3.1.1 find()和rfind()
string = "hello, hello, world!"

# 使用 find() 方法查找子字符串 "hello"
index1 = string.find("hello")

# 使用 rfind() 方法查找子字符串 "hello"
index2 = string.rfind("hello")

# 使用 find() 方法查找不存在的子字符串 "python"
index3 = string.find("python")

# 使用 rfind() 方法查找不存在的子字符串 "python"
index4 = string.rfind("python")

print(index1, index2, index3, index4)

# 3.1.2 index()和rindex()
string = "hello, hello, world!"

# 使用 find() 方法查找子字符串 "hello"
index1 = string.index("hello")

# 使用 rfind() 方法查找子字符串 "hello"
index2 = string.rindex("hello")

# 使用 find()和rfind 方法查找不存在的子字符串 "python"
index3 = string.index("python")
#index4 = string.rindex("python")

print(index1, index2)

# 3.1.3 count()
my_string = "Hello, World, Hello"
print(my_string.count("Hello"))

# 3.1.4 startswith() 和 endswith()

my_string = "Hello, World"

# 检查是否以指定的前缀开头
print(my_string.startswith("Hello"))  # 输出: True
print(my_string.startswith("World"))  # 输出: False

# 检查是否以指定的后缀结尾
print(my_string.endswith("World"))    # 输出: True
print(my_string.endswith("Hello"))    # 输出: False

# 3.2 字符串替换
s = "Hello, World and up and good and Python and and aaaa and"
start = 0
count = s.count("and")
print(count)

s1 = s.replace("and", "ands", count)
print(s1)

# 3.3 字符串拆分
s = "Hello, World and up and good and Python and and aaaa and"
words = s.split(sep=" ")
print(words)

# 3.4 ord 和 chr

# 使用 ord() 将字符转换为 ASCII 码值
print(ord('A'))  # 输出: 65
print(ord('a'))  # 输出: 97

# 使用 chr() 将 ASCII 码值转换为字符
print(chr(65))   # 输出: 'A'
print(chr(97))   # 输出: 'a'

# 3.5 计算字符串长度
my_string = "Hello, World!"
length = len(my_string)
print(length)

# 3.6 字符串大小写转换
my_string = "hello, world! HELLO \t"
upper_str = my_string.upper()
lower_str = my_string.lower()
capitalize_str = my_string.capitalize()
title_str = my_string.title()
swap_str = my_string.swapcase()

print(upper_str,lower_str,capitalize_str,title_str)
print(swap_str)

# 3.7 字符串去除空白
my_string = "   hello, world!   "
strip_str = my_string.strip()
left_strip_str = my_string.lstrip()
right_strip_str = my_string.rstrip()

print(strip_str, '\n', left_strip_str, '\n', right_strip_str)
# 3.8 判断字符串函数
# 省略，没举例子所以没写

# join合并字符串

my_list = ["apple", "banana", "cherry"]
separator = ", "
result = separator.join(my_list)
print(result)

